(function () {

	'use strict';

	angular
		.module('app')
		.controller('AddressEditController', AddressEditController);

	AddressEditController.$inject = ['$scope', '$http','API', '$location', '$q', '$window', '$state', '$stateParams', 'AddressEditModel'];

	function AddressEditController($scope, $http,API, $location, $q, $window, $state, $stateParams, AddressEditModel) {

		$scope.touchSave = function () {
			var address_id = $scope.address_id;
            var receiver_name = $scope.receiver_name;
            var mobile = $scope.mobile;
            var full_address = $scope.full_address;
            var regions = $scope.regions;

            if (!receiver_name || receiver_name.length < 2) {
                $scope.toast('请输入姓名');
                return;
            }

            if (!mobile || mobile.length != 11) {
                $scope.toast('请输入手机号');
                return;
            }

            if (!regions || !regions.length) {
                $scope.toast('请选择地区');
                return;
            }

            var lastRegion = regions[regions.length - 1];
            if (!lastRegion) {
                $scope.toast('请选择地区');
                return;
            }

            if (!full_address || full_address.length < 1) {
                $scope.toast('请输入地址');
                return;
            }

            if ( $scope.allowSave ) {// 防止多次的保存
                $scope.allowSave = false;
            } else {
                return;
            }

            $scope.addressEditModel.save(address_id, receiver_name, mobile, lastRegion.region_id, full_address).then(function (res) {
                $scope.allowSave = true;
                if (res) {
                    $scope.toast('保存成功');
                    $scope.goBack();
                } else {
                    $scope.toast('请稍后再试');
                }
            })
        };

		$scope.touchSetDefault = function() {
            $scope.isDefault = !$scope.isDefault;
        };

		$scope.touchPickerShow = function () {
            $scope.pickerData = [];
            $scope.pickerRegions = [];
            $scope.pickerRegionName = null;

            $scope.showPicker = true;

            $scope.addressEditModel.reloadIfNeeded().then(function (success) {
                if (success) {
                    $scope.pickerData = $scope.addressEditModel.regions;
                } else {
                    $scope.toast('请稍后再试');
                    $scope.touchPickerCancel();
                }
            });
        };

		$scope.touchPickerRegion = function (region) {
            $scope.pickerRegions.push(region);
            $scope.pickerRegionName = _formatRegions($scope.pickerRegions);

            if (region.regions && region.regions.length) {
                $scope.pickerData = region.regions;
            } else {
                $scope.regions = $scope.pickerRegions;
                $scope.showPicker = false;
            }
        };

		$scope.touchPickerCancel = function () {
            $scope.showPicker = false;

            $scope.pickerData = [];
            $scope.pickerRegions = [];
            $scope.pickerRegionName = null;
        };

		$scope.touchDelete = function(address_id){
            API.consignee.deleteAdd({address_id: address_id}).then(function (res) {
                if(res){
                    $scope.toast('删除成功');
                    $scope.goBack();
                }
                else{
                    $scope.toast('删除失败');
                }

            });
        };

		$scope.touchCancel = function (){
            $scope.goBack();
        };

        $scope.formatRegions = _formatRegions;

		$scope.showPicker = false;
		$scope.allowSave = true;
		$scope.pickerData = [];
		$scope.pickerRegions = [];
		$scope.pickerRegionName = null;

		$scope.addressEditModel = AddressEditModel;

		var address = $scope.addressEditModel.address;
		if (address && address.address_id) {
			$scope.address_id = address.address_id;
			$scope.receiver_name = address.receiver_name;
			$scope.mobile = address.mobile;
            var strs = address.address_code.split(",");
            $scope.regions = [{region_id:strs[1], region_name:address.province}, {region_id:strs[2], region_name:address.city}, {region_id:strs[3], region_name:address.district}];
			$scope.full_address = address.full_address;
			$scope.isDefault = address.is_default;
		}

		function _formatRegions(regions) {
			var address = '';
			for (var i = 0; i < regions.length; ++i) {
				address += regions[i].region_name;
				address += ' ';
			}

			return address;
		}
	}

})();
